package org.odk.clinic.adroid.service;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.odk.clinic.android.openmrs.Constants;

public class Concept {
	public ArrayList<String> concept;
	public ArrayList<String> conceptid;
	int NumCount;
	
	String ServicePage = "GetAllConcept.asmx";
	String METHOD_NAME = "GetallConcept";
	
	public Concept GetAllConcept(){
		SoapObject table = null; // Table hold data get back form service
		SoapObject client = null;
		SoapObject tableRow = null; // Row of table
		SoapObject responseBody = null; //Content file XML
		
		AndroidHttpTransport transport = null; // Call service
		SoapSerializationEnvelope sse = null;
		
		sse = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		sse.addMapping(Constants.NAMESPACE, "Concept", this.getClass());
		sse.dotNet = true;
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(Constants.URL + ServicePage);
		Concept con = new Concept();
		
		try{
			client = new SoapObject(Constants.NAMESPACE, METHOD_NAME);
			sse.setOutputSoapObject(client);
			sse.bodyOut = client;
			androidHttpTransport.call(Constants.SOAP_ACTION + METHOD_NAME, sse);
			//Get all file XML
			responseBody = (SoapObject)sse.getResponse();
			//Reject information XML and get back result
			responseBody = (SoapObject)responseBody.getProperty(1);
			//Get information of back table
			table = (SoapObject)responseBody.getProperty(0);
			//Count records in file XML
			con.NumCount = table.getPropertyCount();
			con.concept = new ArrayList<String>();
			con.conceptid = new ArrayList<String>();
			for(int i = 0; i < con.NumCount; i++)
			{
				tableRow = (SoapObject)table.getProperty(i);
				con.concept.add(tableRow.getProperty("name").toString());
				con.conceptid.add(tableRow.getProperty("concept_id").toString());
			}
			return con;
		}catch(Exception e)
		{
			//e.printStackTrace();
			con.concept.add(e.toString());
			con.conceptid.add(e.toString());
			con.NumCount = 0;
			return con;
		}
	}
}
